﻿namespace MicroCloud.Message.Database
{
    /// <summary>
    /// 消息接收者实体映射配置
    /// </summary>
    public class MessageReceiverConfiguration : EntityTypeConfigurationBase<MessageReceiver, long>
    {
        #region "方法"
        #region "重写以实现实体类型各个属性的数据库配置"
        /// <summary>
        /// 重写以实现实体类型各个属性的数据库配置
        /// </summary>
        /// <param name="builder">实体类型创建器</param>
        public override void Configuration(EntityTypeBuilder<MessageReceiver> builder)
        {
            //builder.HasIndex(m => new { m.MessageId, m.ReceiverId }).HasDatabaseName("IDX_MessageReceiver_ReceiverId").IsUnique();
            //builder.HasIndex(m => m.LastReadTime).HasDatabaseName("IX_MessageReceiver_LastReadTime").IsUnique(false).HasFilter(null);
            builder.HasIndex(m => new { m.MessageId, m.ReceiverId }).SetIndex("MessageReceiverId", true);
            builder.HasIndex(m => m.LastReadTime).SetIndex("LastReadTime", false).HasFilter(null);

            //builder.HasOne(ur => ur.Message).WithMany(r => r.Receivers).HasForeignKey(m => m.MessageId).HasConstraintName("FK_MessageReceiver_MessageId").IsRequired();
            //builder.HasOne(m => m.Receiver).WithMany().HasForeignKey(m => m.ReceiverId).HasConstraintName("FK_MessageReceiver_ReceiverId").OnDelete(DeleteBehavior.NoAction).IsRequired();
            builder.HasOne(ur => ur.Message).WithMany(r => r.Receivers).HasForeignKey(m => m.MessageId).SetForeignKey("MessageId");

            //builder.HasQueryFilter(b => b.Receiver.DeletedTime == null);

            ConfigurationAppend(builder);
        }
        #endregion

        #endregion

    }

}
